package com.shop.shoporder.wapController;

import com.shop.shoporder.common.HttpRequest;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/wap")
public class LogisticsController {
    @Autowired
    LogisticsService logisticsService;
    //发起物流推送订阅
//    @ResponseBody
//    @RequestMapping("/getExpressInfo")
//    public Response getExpressInfo() {
////            Config config = configService.selectByPrimaryKey(1);
////            String expressCompanyCode = config.getExpressCompanyCode();
////            String expressKey = config.getExpressKey();
////            String expressApiUrl = config.getExpressApiurl();
//
//        String expressCompanyCode = "huitongkuaidi";
//        String expressKey = "nnzeDXbl6758";
//        String expressApiUrl = "http://www.kuaidi100.com/";
//
//        //模拟物流单号数据
//        TaskRequest req = new TaskRequest();
//        req.setCompany(expressCompanyCode);
////            req.setFrom("贵州省贵阳市观山湖区");
////            req.setTo("山西太原市小店区");
//        req.setNumber("51237107105101");
//        req.getParameters().put("callbackurl", "http://www.ycsqd.com/wap/doPostLogistics");
//        req.setKey(expressKey);
//
//        HashMap<String, String> p = new HashMap<String, String>();
//        p.put("schema", "json");
//        p.put("param", JacksonHelper.toJSON(req));
//        try {
//            String ret = HttpRequest.postData(expressApiUrl+"poll", p, "UTF-8");
//            TaskResponse resp = JacksonHelper.fromJSON(ret, TaskResponse.class);
//            System.out.println(resp);
//            System.out.println(resp.toXml());
//            if (resp.getResult() == true) {
//                System.out.println("订阅成功");
//                return new Response(MessageConstants.MSG_SUCCESS, 1, 1, "订阅成功!");
//            } else {
//                System.out.println("订阅失败");
//                return new Response(MessageConstants.MSG_SUCCESS, 1, "订阅失败");
//            }
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//            return new Response(MessageConstants.MSG_SUCCESS, 1, "异常"+e.getMessage());
//        }
//    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getExpressNoData",method = RequestMethod.POST)
    public Response getExpressNoData(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=logisticsService.getExpressNoData(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
        return response;
    }
}
